/*
 * Copyright 2016-2017 lei.xu<xulei2008xulei@163.com>.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.san21go.jfinfunc.util;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;

import org.joda.time.DateTime;
import org.joda.time.Interval;
import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.joda.time.format.DateTimeFormat;

/**
 * @Description: 日期工具类
 *
 * @version V1.0
 */
public class DateUtil {
	public static final String HUNDSUN_VERSION = "@system 理财资产管理系统V2.2 @version v2.2.0.1 @lastModiDate 2016年7月25日 @describe  ";

	public static String DEFAULT_FORMAT_DATE = "yyyyMMdd";

	static Map<String, Pattern> dateRegFormatPattern = new HashMap<String, Pattern>();
	static HashMap<String, String> dateRegFormat = new LinkedHashMap<String, String>();
	static {
		dateRegFormat.put("^\\d{8}$", "yyyyMMdd");// 20140312
		dateRegFormat.put("^\\d{4}-{1}\\d{2}-{1}\\d{2}$", "yyyy-MM-dd");// 2014-03-12
		dateRegFormat.put("^\\d{4}/{1}\\d{2}/{1}\\d{2}$", "yyyy/MM/dd");// 2014/03/12
		dateRegFormat.put("^\\d{4}/{1}\\d{1}/{1}\\d{2}$", "yyyy/M/dd");// 2014/3/12
		dateRegFormat.put("^\\d{4}/{1}\\d{2}/{1}\\d{1}$", "yyyy/MM/d");// 2014/03/1
		dateRegFormat.put("^\\d{4}/{1}\\d{1}/{1}\\d{1}$", "yyyy/M/d");// 2014/3/1
		dateRegFormat.put("^\\d{2}-{1}\\d{1,2}-{1}\\d{1,2}$", "yy-MM-dd");// 14.10.18(年.月.日)
		dateRegFormat.put("^\\d{1,2}\\D+\\d{1,2}\\D+\\d{4}$", "dd-MM-yyyy");// 12.21.2013(日.月.年)

		for (String key : dateRegFormat.keySet()) {
			dateRegFormatPattern.put(key, Pattern.compile(key));
		}
	}

	/**
	 * 判断日期前后关系
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static boolean isBefore(String startDate, String endDate) {
		DateTime endDateTime = formatDate(endDate);
		DateTime startDateTime = formatDate(startDate);

		boolean ret = startDateTime.isBefore(endDateTime.getMillis());
		return ret;
	}

	/**
	 * 判断日期前后关系
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static boolean isAfter(String startDate, String endDate) {
		DateTime endDateTime = formatDate(endDate);
		DateTime startDateTime = formatDate(startDate);

		boolean ret = startDateTime.isAfter(endDateTime.getMillis());
		return ret;
	}

	/**
	 * 日期之差，返回单位为日
	 * 
	 * @return
	 * @author xulei
	 */
	public static int diffDate(String endDate, String startDate) {
		DateTime endDateTime = formatDate(endDate);
		DateTime startDateTime = formatDate(startDate);

		Interval interval = new Interval(startDateTime, endDateTime);
		Period ret = interval.toPeriod(PeriodType.days());
		return ret.getDays();
	}

	/**
	 * 日期之差，返回单位为月
	 * 
	 * @return
	 * @author xulei
	 */
	public static int diffMonth(String endDate, String startDate) {
		DateTime endDateTime = formatDate(endDate);
		DateTime startDateTime = formatDate(startDate);

		Interval interval = new Interval(startDateTime, endDateTime);
		Period ret = interval.toPeriod(PeriodType.months());
		return ret.getMonths();
	}

	/**
	 * 日期之差，返回单位为年
	 * 
	 * @return
	 * @author xulei
	 */
	public static int diffYear(String endDate, String startDate) {
		DateTime endDateTime = formatDate(endDate);
		DateTime startDateTime = formatDate(startDate);

		Interval interval = new Interval(startDateTime, endDateTime);
		Period ret = interval.toPeriod(PeriodType.years());
		return ret.getYears();
	}

	public static DateTime formatDate(String dateStr) {
		DateTime datetime = null;
		for (Entry<String, Pattern> entry : dateRegFormatPattern.entrySet()) {
			if (entry.getValue().matcher(dateStr).matches()) {
				datetime = DateTimeFormat.forPattern(dateRegFormat.get(entry.getKey())).parseDateTime(dateStr);
			}
		}
		if (datetime == null) {
			throw new UnsupportedOperationException("日期格式不支持");
		}
		return datetime;
	}

	public static String formatDateStr(String dateStr) {
		String strSuccess = "";
		for (Entry<String, Pattern> entry : dateRegFormatPattern.entrySet()) {
			if (entry.getValue().matcher(dateStr).matches()) {
				DateTime datetime = DateTimeFormat.forPattern(dateRegFormat.get(entry.getKey())).parseDateTime(dateStr);
				strSuccess = datetime.toString(DEFAULT_FORMAT_DATE);
			}
		}
		if (strSuccess == null || strSuccess.length() == 0) {
			throw new UnsupportedOperationException("日期格式不支持");
		}
		return strSuccess;
	}

}
